home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htsthread.c < prev    next >
C/C++ Source or Header  |  2007-02-03  |  5KB  |  210 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Threads                                                */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. /* Internal engine bytecode */
  38. #define HTS_INTERNAL_BYTECODE
  39.  
  40. #include "htsglobal.h"
  41. #include "htsbase.h"
  42. #include "htsthread.h"
  43.  
  44. #if USE_BEGINTHREAD
  45. #ifdef _WIN32
  46. #include <process.h>
  47. #endif
  48. #endif
  49.  
  50. static int process_chain = 0;
  51. static htsmutex process_chain_mutex = HTSMUTEX_INIT;
  52.  
  53. HTSEXT_API void htsthread_wait(void ) {
  54.   htsthread_wait_n(0);
  55. }
  56.  
  57. HTSEXT_API void htsthread_wait_n(int n_wait) {
  58. #if USE_BEGINTHREAD
  59.   int wait = 0;
  60.   do {
  61.     hts_mutexlock(&process_chain_mutex);
  62.     wait = (process_chain > n_wait );
  63.     hts_mutexrelease(&process_chain_mutex);
  64.     if (wait)
  65.       Sleep(100);
  66.   } while(wait);
  67. #endif
  68. }
  69.  
  70. /* ensure initialized */
  71. HTSEXT_API void htsthread_init(void ) {
  72. #if USE_BEGINTHREAD
  73. #if (defined(_DEBUG) || defined(DEBUG))
  74.   assertf(process_chain == 0);
  75. #endif
  76.   if (process_chain_mutex == HTSMUTEX_INIT) {
  77.     hts_mutexinit(&process_chain_mutex);
  78.   }
  79. #endif
  80. }
  81.  
  82. HTSEXT_API void htsthread_uninit(void ) {
  83.   htsthread_wait();
  84. #if USE_BEGINTHREAD
  85.   hts_mutexfree(&process_chain_mutex);
  86. #endif
  87. }
  88.  
  89. typedef struct hts_thread_s {
  90.   void *arg;
  91.   void (*fun)(void *arg);
  92. } hts_thread_s;
  93.  
  94. #ifdef _WIN32
  95. static unsigned int __stdcall hts_entry_point(void *tharg)
  96. #else
  97. static void* hts_entry_point(void *tharg)
  98. #endif
  99. {
  100.   hts_thread_s *s_args = (hts_thread_s*) tharg;
  101.   void * const arg = s_args->arg;
  102.   void (*fun)(void *arg) = s_args->fun;
  103.   free(tharg);
  104.  
  105.   hts_mutexlock(&process_chain_mutex);
  106.   process_chain++;
  107.   assertf(process_chain > 0);
  108.   hts_mutexrelease(&process_chain_mutex);
  109.  
  110.   /* run */
  111.   fun(arg);
  112.  
  113.   hts_mutexlock(&process_chain_mutex);
  114.   process_chain--;
  115.   assertf(process_chain >= 0);
  116.   hts_mutexrelease(&process_chain_mutex);
  117. #ifdef _WIN32
  118.   return 0;
  119. #else
  120.   return NULL;
  121. #endif
  122. }
  123.  
  124. /* create a thread */
  125. HTSEXT_API int hts_newthread( void (*fun)(void *arg), void *arg)
  126. {
  127.   hts_thread_s *s_args = malloc(sizeof(hts_thread_s));
  128.   assertf(s_args != NULL);
  129.   s_args->arg = arg;
  130.   s_args->fun = fun;
  131. #ifdef _WIN32
  132.   {
  133.     unsigned int idt;
  134.     HANDLE handle = (HANDLE) _beginthreadex(NULL, 0, hts_entry_point, s_args, 0, &idt);
  135.     if (handle == 0) {
  136.       free(s_args);
  137.       return -1;
  138.     } else {
  139.       /* detach the thread from the main process so that is can be independent */
  140.       CloseHandle(handle);
  141.     }
  142.   }
  143. #else
  144.   {
  145.     pthread_t handle = 0;
  146.     int retcode = pthread_create(&handle, NULL, hts_entry_point, s_args);
  147.     if (retcode != 0) {   /* error */
  148.       free(s_args);
  149.       return -1;
  150.     } else {
  151.       /* detach the thread from the main process so that is can be independent */
  152.       pthread_detach(handle);
  153.     }
  154.   }
  155. #endif
  156.   return 0;
  157. }
  158.  
  159. #if USE_BEGINTHREAD
  160.  
  161. /* Note: new 3.41 cleaned up functions. */
  162.  
  163. HTSEXT_API void hts_mutexinit(htsmutex* mutex) {
  164.   htsmutex_s* smutex = malloct(sizeof(htsmutex_s));
  165. #ifdef _WIN32
  166.   smutex->handle = CreateMutex(NULL, FALSE, NULL);
  167. #else
  168.   pthread_mutex_init(&smutex->handle, 0);
  169. #endif
  170.   *mutex = smutex;
  171. }
  172.  
  173. HTSEXT_API void hts_mutexfree(htsmutex* mutex) {
  174.   if (mutex != NULL && *mutex != NULL) {
  175. #ifdef _WIN32
  176.     CloseHandle((*mutex)->handle);
  177. #else
  178.     pthread_mutex_destroy(& ( (*mutex)->handle ) );
  179. #endif
  180.     freet(*mutex);
  181.     *mutex = NULL;
  182.   }
  183. }
  184.  
  185. HTSEXT_API void hts_mutexlock(htsmutex* mutex) {
  186.   assertf(mutex != NULL);
  187.   if (*mutex == HTSMUTEX_INIT) {   /* must be initialized */
  188.     hts_mutexinit(mutex);
  189.   }
  190.   assertf(*mutex != NULL);
  191. #ifdef _WIN32
  192.   assert((*mutex)->handle != NULL);
  193.   WaitForSingleObject((*mutex)->handle, INFINITE);
  194. #else
  195.   pthread_mutex_lock(&(*mutex)->handle);
  196. #endif
  197. }
  198.  
  199. HTSEXT_API void hts_mutexrelease(htsmutex* mutex) {
  200.   assertf(mutex != NULL && *mutex != NULL);
  201. #ifdef _WIN32
  202.   assert((*mutex)->handle != NULL);
  203.   ReleaseMutex((*mutex)->handle);
  204. #else
  205.   pthread_mutex_unlock(&(*mutex)->handle);
  206. #endif
  207. }
  208.  
  209. #endif
  210.